package edu.bath.sensorvisualiser;

import edu.bath.sensorframework.Visualisation;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;

public class RoomTemperature implements Visualisation<Float> {
	private Canvas c;
	public RoomTemperature(Canvas c, Context ctxt) {
		this.c = c;
		
    	Bitmap thermo = BitmapFactory.decodeResource(ctxt.getResources(), R.drawable.termo);
    	c.drawBitmap(thermo, 0, 0, null);
    	Paint p = new Paint();
    	p.setStyle(Style.FILL);
    	p.setARGB(255, 255, 0, 0);
    	c.drawRect(20, 253, 41, 261, p);
	}
	
	Float temp = 0.0f;
	
	@Override
	public void draw() {
		if(temp > 40.0f)
			temp = 40.0f; // this thermometer is designed for BRITAIN!
		
    	Paint red = new Paint();
    	red.setStyle(Style.FILL);
    	red.setARGB(255, 255, 0, 0);
    	
    	Paint white = new Paint();
    	white.setStyle(Style.FILL);
    	white.setARGB(255, 255, 255, 255);
    	
    	// First clear the thermometer
		c.drawRect(20, 17, 41, 253, white);
		
		// Calculate top
		float unit = 22.0f/4.0f; // pixels per 1C
		
		int top = Math.round(253.0f-(unit*temp));
		c.drawRect(20, top, 41, 253, red);
	}

	@Override
	public void addData(Float data) {
		this.temp = data;
	}

	@Override
	public void flushData() {
		this.temp = 0.0f;
	}
}
